package org.springextensions.actionscript.mvcs.service.operation
{
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	
	import org.springextensions.actionscript.mvcs.service.operation.event.AsyncOperationErrorEvent;
	import org.springextensions.actionscript.mvcs.service.operation.event.AsyncOperationEvent;
	import org.springextensions.actionscript.mvcs.service.operation.event.AsyncOperationResultEvent;
	import org.springextensions.actionscript.mvcs.service.operation.events.OperationProgressEvent;

	public class AbstractOperation extends EventDispatcher implements IOperation
	{
		public function get progress():Number 
		{
			return 0;
		}
		
		private var _displayName:String;
		
		public function get displayName():String 
		{
			return _displayName;
		}
		
		public function set displayName(value:String):void 
		{
			_displayName = value;
		}
		
		public function get result():*
		{
			return null;
		}
		
		public function AbstractOperation(target:IEventDispatcher=null)
		{
			super(target);
		}
		
		public function execute():void
		{
		}
		
		public function isComplete():Boolean
		{
			return false;
		}
		
		public function canAct():Boolean 
		{
			return true;
		}
		
		public function addResultListener(listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void
		{
			addEventListener(AsyncOperationEvent.RESULT, listener, useCapture, priority, useWeakReference);
		}
		
		public function addErrorListener(listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void
		{
			addEventListener(AsyncOperationEvent.ERROR, listener, useCapture, priority, useWeakReference);
		}
		
		public function addProgressListener(listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void
		{
			addEventListener(OperationProgressEvent.PROGRESS, listener, useCapture, priority, useWeakReference);
		}
		
		public function dispatchResult(result:*):Boolean
		{
			return dispatchEvent(new AsyncOperationResultEvent(result));
		}
		
		public function dispatchError(error:*):Boolean
		{
			return dispatchEvent(new AsyncOperationErrorEvent(error));
		}		
		
		public function dispatchProgress(progress:*):Boolean 
		{
			return dispatchEvent(new OperationProgressEvent(progress));
		}
	}
}